﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            IndexerClass Indexer = new IndexerClass();
            Indexer[0] = "0";
            Indexer[1] = "1";

            Indexer[3] = "3";
            Indexer[23] = "23";
            //输出索引器的值，其实就是调用其get方法
            Console.WriteLine(Indexer[0]);
            Console.WriteLine(Indexer[1]);


            var c2 = new IndexerClass2();
            c2["a"] = "a";

            c2["a2"] = "a2";
            Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(c2));
            Console.Read();
        }
    }

    public class IndexerClass
    {
        private string[] name = new string[2];

        //索引器必须以this关键字定义，其实这个this就是类实例化之后的对象
        public string this[int index]
        {
            //实现索引器的get方法
            get
            {
                if (index < 2)
                {
                    return name[index];
                }
                return null;
            }

            //实现索引器的set方法
            set
            {
                if (index < 2)
                {
                    name[index] = value;
                }
            }
        }
    }

    public class IndexerClass2
    {
        //用string作为索引器下标的时候，要用Hashtable
        private Hashtable name = new Hashtable();

        //索引器必须以this关键字定义，其实这个this就是类实例化之后的对象
        public string this[string index]
        {
            get
            {
                return name[index].ToString();
            }
            set { name.Add(index, value); }
        }
    }

    /// <summary>
    /// 索引重载
    /// </summary>
    public class IndexerClass3
    {
        private Hashtable name = new Hashtable();

        //1：通过key存取Values
        public string this[int index]
        {
            get { return name[index].ToString(); }
            set { name.Add(index, value); }
        }

        //2：通过Values存取key
        public int this[string aName]
        {
            get
            {
                //Hashtable中实际存放的是DictionaryEntry(字典)类型，如果要遍历一个Hashtable，就需要使用到DictionaryEntry
                foreach (DictionaryEntry d in name)
                {
                    if (d.Value.ToString() == aName)
                    {
                        return Convert.ToInt32(d.Key);
                    }
                }
                return -1;
            }
            set
            {
                name.Add(value, aName);
            }
        }
    }
    #region  多参索引器

    //入职信息类
    public class EntrantInfo
    {
        //姓名、编号、部门
        private string name;
        private int number;
        private string department;
        public EntrantInfo()
        {

        }
        public EntrantInfo(string name, int num, string department)
        {
            this.name = name;
            this.number = num;
            this.department = department;
        }

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public int Num
        {
            get { return number; }
            set { number = value; }
        }

        public string Department
        {
            get { return department; }
            set { department = value; }
        }
    }

    //声明一个类EntrantInfo的索引器
    public class IndexerForEntrantInfo
    {
        private ArrayList ArrLst;//用于存放EntrantInfo类
        public IndexerForEntrantInfo()
        {
            ArrLst = new ArrayList();
        }

        //声明一个索引器：以名字和编号查找存取部门信息
        public string this[string name, int num]
        {
            get
            {
                foreach (EntrantInfo en in ArrLst)
                {
                    if (en.Name == name && en.Num == num)
                    {
                        return en.Department;
                    }
                }
                return null;
            }
            set
            {
                //new关键字：C#规定，实例化一个类或者调用类的构造函数时，必须使用new关键
                ArrLst.Add(new EntrantInfo(name, num, value));
            }
        }

        //声明一个索引器：以编号查找名字和部门
        public ArrayList this[int num]
        {
            get
            {
                ArrayList temp = new ArrayList();
                foreach (EntrantInfo en in ArrLst)
                {
                    if (en.Num == num)
                    {
                        temp.Add(en);
                    }
                }
                return temp;
            }
        }

        //还可以声明多个版本的索引器...
    }
    #endregion

}
